//
//  ShouCangVController.m
//  wdawdawdw
//
//  Created by 吕超 on 16/11/17.
//  Copyright © 2016年 lc. All rights reserved.
//

#import "ShouCangVController.h"
#import "LXTableViewCell.h"
#import "DataBase.h"
#import "LXModel.h"



#define SuiJiYanSe [UIColor colorWithRed:arc4random()%256/255.0 green:arc4random()%256/255.0 blue:arc4random()%256/255.0 alpha:1]

#define selfPingMuWidth [UIScreen mainScreen].bounds.size.width
#define selfPingMuHeight [UIScreen mainScreen].bounds.size.height

@interface ShouCangVController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong)UITableView *mainTableView;



@end

@implementation ShouCangVController

- (void)viewDidLoad {
    [super viewDidLoad];

    //将原点移动到navigationBar
    self.edgesForExtendedLayout = UIRectEdgeNone;
    
    self.view.backgroundColor = [UIColor redColor];
    
    // 初始化 tableView
    _mainTableView = [[UITableView alloc]initWithFrame:[UIScreen mainScreen].bounds];
    // 代理  和  数据源
    self.mainTableView.delegate = self;
    self.mainTableView.dataSource = self;
    
    [self.view addSubview:self.mainTableView];
    
    [self.mainTableView registerClass:[LXTableViewCell class] forCellReuseIdentifier:@"222"];
    
    //行高
    _mainTableView.rowHeight = 230;
    

}


// 每次  进入更新
- (void)viewWillAppear:(BOOL)animated{
    self.SCArray = [[DataBase ShareDataBase]getAllData];
    
    [self.mainTableView reloadData];
}

//  行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.SCArray.count;
}



- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    LXTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"222"];
    
    if(cell == nil){
        cell = [[LXTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"222"];
    }
    // 选择 图片 和 输入 都禁止使用
    cell.selectBtn.enabled = NO;
    
    cell.numberTF.enabled = NO;
    
    LXModel *model = self.SCArray[indexPath.row];
    
    
    cell.LXLabel.text = model.alias;
    
    cell.numberTF.text = model.number;
    
    cell.priceLabel.text = [NSString stringWithFormat:@"%ld",model.DanGeJiaGe];

    
    cell.PictureImage.image  = [UIImage imageWithData:model.picture];
    
    return cell;
}




// cell  删除 
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (editingStyle == UITableViewCellEditingStyleDelete){
        
        LXModel *model = self.SCArray[indexPath.row];
        
        // 删除 数据库中数据
        [[DataBase ShareDataBase]deleteDataBase:model];
        

        // 数组中删除当前行 
        [self.SCArray removeObjectAtIndex:indexPath.row];

        [self.mainTableView reloadData];
        
    }
}


- (NSMutableArray *)SCArray{
    if (_SCArray == nil){
        _SCArray = [NSMutableArray array];
    }
    return _SCArray;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}





@end
